﻿#include "login.h"
#include "ui_login.h"
#include"register.h"
#include"odbclib.h"
#include"clientfile.h"
#include"clientwidget.h"
#include"tcpserver.h"
#include"tcpclient.h"
#include<QMessageBox>
#include<QDebug>
#include <QDateTime>
bool login::ispoenserver=false;

login::login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::login)
{
    ui->setupUi(this);

    setWindowTitle(QString::fromLocal8Bit("用户登录"));
     this->setWindowIcon(QIcon(":/photo/11.jfif"));

    //设置皮肤
    setfacer(4);

    QTimer *timer_calendar;//用来显示当前日期的定时器
    timer_calendar = new QTimer(this);//new一个QTimer对象
    connect(timer_calendar,SIGNAL(timeout()),this,SLOT(showtime()));//连接槽函数
    timer_calendar->start(1000);//每一秒溢出一次进入槽函数

}

QString login::getid86()
{
    return id86;
}

QString login::getpasswd86()
{
    return passwd86;
}

void login::set_pushbutton3(bool isopen)
{
    if(isopen==false)
    ui->pushButton_3->setText(QString::fromLocal8Bit("服务器未打开"));
    else
    ui->pushButton_3->setText(QString::fromLocal8Bit("服务器未打开"));

}

login::~login()
{
    delete ui;
}

void login::setfacer(int index)
{
    //换背景
    QImage _image;   //创建image
   //利用字符串拼接 完成photo
    QString str=QString(":/photo/back%1.jpg").arg(index);
   _image.load(str);
   setAutoFillBackground(true);   // 这个属性一定要设置
    QPalette pal(palette());
     pal.setBrush(QPalette::Window, QBrush(_image.scaled(size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    setPalette(pal);
}

void login::on_pushButton_2_clicked()
{
    QString id=ui->nametext->text();
    QString passwd=ui->passwdtext->text();

    ODBClib *o=new ODBClib;
    QSqlDatabase db=o->connectdb();
     QSqlQuery qsQuery=QSqlQuery(db);

     if(tcpserver::isonline()==false)  //如果没连接上先查询管理员账号在查询其他
     {
         QString server_str=QString("select * from manager where id='%1' and passwd='%2'").arg(id).arg(passwd);
         qsQuery.exec(server_str);
         qsQuery.last();
         int serverindex1=qsQuery.at()+1;
         if(serverindex1==1){
             QMessageBox::information(NULL,QString::fromLocal8Bit("登录验证"),QString::fromLocal8Bit("登录成功"));
             //传入父窗口
             tcpserver *s1=new tcpserver(this);
             s1->connect_server();
             s1->show();
             ui->pushButton_3->setText(QString::fromLocal8Bit("服务器已经打开"));
             QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("你已经进入后台，请谨慎操作！！！！"));
             return;
         }
     }


    QString str=QString("select * from myqicq_user where id='%1' and passwd='%2'").arg(id).arg(passwd);
    qsQuery.exec(str);
    qsQuery.last();
    int index1=qsQuery.at()+1;
    if(ui->nametext->text()==""||ui->passwdtext->text()==""){

        QMessageBox::information(NULL,QString::fromLocal8Bit("登录验证"),QString::fromLocal8Bit("输入信息不完整，登录失败"));
        return;
    }

    if(tcpserver::isonline()==false){
        QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("服务器未打开!"));
        return;
    }

    if(index1==1){
        QMessageBox::information(NULL,QString::fromLocal8Bit("登录验证"),QString::fromLocal8Bit("登录成功"));

        id86=id;
        passwd86=passwd;

        tcpclient *c1=new tcpclient(this);  //传入本身 后续tcpclient可以操作login中的函数
        c1->set_userName(id);
        c1->connect_client();
        c1->show();
    }
    else {
        qDebug()<<"输出为假"<<endl;
        QMessageBox::information(NULL,QString::fromLocal8Bit("登录验证"),QString::fromLocal8Bit("账号或密码错误，登录失败"));
        return;
    }

}


void login::on_pushButton_2_pressed()
{

}


void login::on_pushButton_pressed()
{

}


void login::on_pushButton_clicked()
{
    Register *r1=new Register;
    r1->show();
}

void login::showtime()
{
    QDateTime time = QDateTime::currentDateTime();
    QString str = time.toString("yyyy-MM-dd hh:mm:ss");
    ui->date->setText(str);

    //设置时间颜色
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::red);
    ui->date->setPalette(pa);
}

